package com.waimai.mobile.ui.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.waimai.mobile.R;
import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.Order;
import com.waimai.mobile.api.model.ResponseModel;
import com.waimai.mobile.misc.Util;
import com.waimai.mobile.ui.WaiMaiApp;
import com.waimai.mobile.ui.activity.MainActivity;
import com.waimai.mobile.ui.activity.OrderDetailActivity;
import com.waimai.mobile.ui.activity.OrderDetailActivity_;
import com.waimai.mobile.ui.adapter.OrderItemAdpater;
import com.waimai.mobile.ui.adapter.RestaurantItemAdpater;
import com.waimai.mobile.ui.listener.RecyclerItemClickListener;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by lin on 2015/5/31.
 */

@EFragment(R.layout.fragment_order_list)
public class OrderListFragment extends Fragment{

    @App
    public WaiMaiApp app;

    @RestService
    public WaiMaiServer server;

    @Bean
    public OrderItemAdpater adapter;

    @ViewById
    public TextView mentionMsgTv;

    @ViewById
    public PtrClassicFrameLayout containerPtr;

    @ViewById
    public RecyclerView orderListRv;

    @AfterViews
    public void afterViews(){

        orderListRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        new RecyclerItemClickListener(getActivity(), orderListRv, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), OrderDetailActivity_.class);
                intent.putExtra("order", adapter.getOrders().get(position));
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        containerPtr.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                getOrders(app.getUser().getUserId());
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if(app.getUser() == null){
            mentionMsgTv.setVisibility(View.VISIBLE);
            mentionMsgTv.setText("请登陆后查看");
            containerPtr.setVisibility(View.GONE);
        }else{
            mentionMsgTv.setVisibility(View.GONE);
            containerPtr.setVisibility(View.VISIBLE);
            containerPtr.autoRefresh(true);
        }
    }

    @UiThread
    public void refreshComplete() {
        containerPtr.refreshComplete();
    }

    @Background
    public void getOrders(Integer userId){
        try{
            ResponseModel<List<Order>> model = server.orders(userId);
            if(model != null){
                switch (model.getStatus().getStatusCode()){
                    case ResponseModel.STATUS_GET_DATA_SUCCESS:
                        getDataSuccess(model.getData());
                        break;
                    case ResponseModel.STATUS_GET_DATA_NONE:
                        getDataNone();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                        break;
                }
            }else{
                serverError();
            }
        }catch (Exception e){
            e.printStackTrace();
            netError();
        }
    }


    @UiThread
    public void getDataSuccess(List<Order> orders){
        adapter.setOrders(orders);
        orderListRv.setAdapter(adapter);
        containerPtr.refreshComplete();
    }

    @UiThread
    public void getDataNone(){
        containerPtr.refreshComplete();
        mentionMsgTv.setVisibility(View.VISIBLE);
        mentionMsgTv.setText("您还有订单哦，赶紧去下单吧");
        containerPtr.setVisibility(View.GONE);
    }

    @UiThread
    public void serverError(){
        containerPtr.refreshComplete();
        Toast.makeText(getActivity(), "服务器错误", Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void netError(){
        Toast.makeText(getActivity(), "网络错误", Toast.LENGTH_SHORT).show();
        containerPtr.refreshComplete();
    }
}
